import { Injectable, Inject } from '@angular/core';
import { Http, Response, Headers } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/Rx';

@Injectable()
export class SexyService {

    private domain: string = "http://www.tngou.net/tnfs/api";

    private API: any = {
        classify: "/classify",
        list: "/list",
        show: "/show"
    }

    constructor(private http: Http) {
    }

    httpGet(url, params: any = {}) {
        return this.http.get(this.domain + url).map(
            res => res.json() == null ? "[]" : res.json()
        ).catch(this.handleError);
    }

    // 获取抽屉菜单
    getMenuItems() {
        return this.httpGet(this.API.classify);
    }

    // 获取列表
    getGalleries(id) {
        let url = this.API.list + "?id=" + id;
        return this.httpGet(url);

    }

    // 获取图片集合
    getDetails(id) {
        let url = this.API.show + "?id=" + id;
        return this.httpGet(url);
    }

    // 处理错误
    private handleError(error: Response) {
        alert("电脑浏览器运行请安装跨域插件！")
        console.log(error);
        return Observable.throw(error.json().error || 'Server Error');
    }
}
